import store from '@/store';
type AuthProps = {
    children: React.ReactNode;
    authKey: string[];
    fallback?: React.ReactNode;
}
function WithAuthComp({ children, authKey, fallback }: AuthProps) {
    const [user] = store.useModel('user');
    // 管理员应有所有权限
    if (user.currentUser?.role === 'admin') return children;
    // 判断是否有权限 
    let hasAuth = false;
    for (let i = 0; i < authKey.length; i++) {
        if (user.currentUser?.role === authKey[i]) {
            hasAuth = true;
            break;
        }
    }
    // 有权限时直接渲染内容
    if (hasAuth) {
        return children;
    } else {
        // 无权限时显示指定 UI
        return fallback || null
    }
}

export default WithAuthComp;